package com.example.execise1.mapper;

import com.example.execise1.dto.HeroInfo;
import com.example.execise1.dto.HeroTypeData;
import com.example.execise1.dto.SkinInfo;
import com.example.execise1.dto.TypeInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface HeroMapper {
    //创建一个皮肤数据功能
    @Insert("INSERT INTO skin_info(skin_name,hero_id,is_default)VALUES(#{skinName}," +
            "#{hero.id},#{isDefault})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    Integer createSkin(SkinInfo skinInfo);

    //更新指定英雄的次要分类API
    @Update("UPDATE hero_info SET minor_type = #{newTypeId} WHERE id = #{id}")
    Integer updateHeroType(@Param("id") Integer id,@Param("newTypeId") Integer newTypeId);
    //根据id获取英雄信息
    @Select("SELECT * FROM hero_info WHERE id = #{id}")
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getHeroInfo(Integer id);

    //统计各类型英雄数量API，按类型id升序排序
    @Select("SELECT b.id,b.type_name,b.type_detail,COUNT(1) hero_count " +
            "FROM hero_info a,type_info b " +
            "WHERE a.chief_type = b.id OR a.minor_type = b.id " +
            "GROUP BY b.id,b.type_name,b.type_detail")
    List<HeroTypeData> getHeroCount();

    //按分类名称查询英雄列表API，分页返回
    @Select("SELECT * FROM type_info WHERE id = #{id}")
    TypeInfo getChiefType(Integer id);

    @Select("SELECT * FROM type_info WHERE id = #{id}")
    TypeInfo getMinorType(Integer id);

    @Select("SELECT a.* " +
            "FROM hero_info a LEFT JOIN type_info b ON a.chief_type = b.id OR a.minor_type = b.id " +
            "WHERE type_name = #{typeName}" +
            "LIMIT #{start},#{size}")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    List<HeroInfo> getTypeName(@Param("typeName") String typeName, @Param("start") Integer start, @Param("size") Integer size);

    @Select("SELECT count(1) totalcount " +
            "FROM hero_info a LEFT JOIN type_info b ON a.chief_type = b.id OR a.minor_type = b.id " +
            "WHERE type_name = #{typeName}")
    Long getHeroTypeCount(String typeName);

    //随机选取五个英雄组成一个队伍
    //战士或坦克
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"战士\" or type_name=\"坦克\") " +
            "ORDER BY RAND() " +
            "LIMIT 1")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getTank();

    //法师
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"法师\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getMage();

    //射手
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"射手\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getStriker();

    //辅助
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"辅助\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getAuxiliary();

    //刺客
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"刺客\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getAssassin();
}